package com.sailorj.itools.utils;

public class CamelCaseUtils {

    public static void main(String[] args) {
        String text = camelCase("process_reset_et_phone_msg_1");
        // processResetEtPhoneMsg1
        System.out.println(text);
        // PROCESS_RESET_ET_PHONE_MSG1
        System.out.println(convert("processResetEtPhoneMsg1"));
    }

    /**
     * 下划线词转为驼峰单词   PROCESS_RESET_ET_PHONE_MSG1 => processResetEtPhoneMsg1
     */
    public static String camelCase(String str) {
        str = str.toLowerCase();
        String[] words = str.split("_");
        StringBuilder camelCaseBuilder = new StringBuilder(words[0]);

        for (int i = 1; i < words.length; i++) {
            String capitalizedWord = words[i].substring(0, 1).toUpperCase() + words[i].substring(1);
            camelCaseBuilder.append(capitalizedWord);
        }
        return camelCaseBuilder.toString();
    }

    /**
     * 驼峰单词转下划线并大写  processResetEtPhoneMsg1 => PROCESS_RESET_ET_PHONE_MSG1
     */
    public static String convert(String camelCase) {
        StringBuilder snakeCase = new StringBuilder();
        for (int i = 0; i < camelCase.length(); i++) {
            char c = camelCase.charAt(i);
            if (Character.isUpperCase(c)) {
                snakeCase.append("_").append(Character.toLowerCase(c));
            } else {
                snakeCase.append(c);
            }
        }
        return snakeCase.toString().toUpperCase();
    }

}
